const { defineConfig } = require("@vue/cli-service");
let { list } = require("./src/mock");
const bodyParser = require("body-parser");
const Mock = require("mockjs");
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    setupMiddlewares(middle, { app }) {
      // 后端接口规范
      // resfull
      app.use(bodyParser.json());
      // 列表查询接口
      app.get("/api/lists", (req, res) => {
        const {
          pageCode = 1, // 页码
          pageSize = 15, // 分页个数
          state = "全部", // 支付状态
          wd = "", // 搜索
          date = "", // 日期查询区间
          desc, // 排序
        } = req.query;
        // 过滤
        const fitlerList = [...list]
          .filter((v) => {
            if (state === "全部") return true;
            return v.state === state;
          })
          .filter((v) => {
            return (
              v.uid.includes(wd) || v.pay.includes(wd) || v.source.includes(wd)
            );
          })
          .filter((v) => {
            if (date === "") return true;
            const start = date.split(",")[0];
            const end = date.split(",")[1];
            const createDateTime = new Date(v.createDateTime).getTime();
            return start <= createDateTime && end >= createDateTime;
          })
          .sort((a, b) => {
            switch (desc) {
              case "WEJKAFLNKALKFLKNAWM;DLKAWNDA;LK":
                return a.price - b.price;
              case "2":
                return b.price - a.price;
            }
          });

        res.send({
          code: 200,
          // 支持分页
          data: fitlerList.slice(
            (pageCode - 1) * pageSize,
            pageCode * pageSize
          ),
          total: fitlerList.length,
        });
      });

      // 列表删除接口[后端动态路由]
      // 支持 单项删除 | 多项删除
      app.delete("/api/list/:uid", (req, res) => {
        // 获取动态路由id
        const { uid } = req.params;
        //  /api/lists/12341231231231
        //  /api/lists/123123123123123,1231231231231,1231231231231,12312312312
        // 通过id进行过滤
        list = list.filter((v) => !uid.includes(v.uid));
        res.send({
          code: 200,
          message: "删除成功！",
          data: null,
        });
      });

      // 添加接口
      app.put("/api/list", (req, res) => {
        req.body.code = list[list.length - 1].code + 1;
        req.body.createDateTime = new Date().toLocaleString();
        req.body.uid = Mock.Random.id();

        list.push(req.body);
        res.send({
          code: 200,
          message: "添加数据成功， 返回列表页面~",
        });
      });
      // 修改编辑接口
      app.post("/api/list", (req, res) => {
        const currItem = list.find((v) => v.uid === req.body.uid);
        Object.assign(currItem, req.body);
        res.send({
          code: 200,
          message: "修改成功~~~~",
        });
      });

      // 图表接口
      app.get("/api/echarts/state", (req, res) => {
        const state = [
          "未付款",
          "已付款",
          "待发货",
          "已发货",
          "代签收",
          "已签收",
          "已关闭",
        ];
        // 计算数量
        const data = state.map((v) => {
          return list.reduce((p, n) => {
            return v === n.state ? p + 1 : p;
          }, 0);
        });

        res.send({
          code: 200,
          data: {
            state,
            data,
          },
        });
      });

      return middle;
    },
  },
});
